/*
 * @Author: 0x9DEFA478
 * @Date: 2022-01-23 15:48:40
 * @LastEditTime: 2022-08-20 21:47:05
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#ifndef __Cartridge_H_NES_Cartridge_H_
#define __Cartridge_H_NES_Cartridge_H_
#include "../H_NES.h"




//Mapper1
typedef struct{
  Hbyte Reg_8000;
  Hbyte Reg_A000;
  Hbyte Reg_C000;
  Hbyte Reg_E000;

  Hbyte LastWrite;
  Hbyte WriteMark;

  struct{
    Hbyte Value;
    Hbyte NumOfBit;
  }LoadRegister;

  struct{
    Hbyte* Base6000_8KB;
    Hbyte* Base8000_16KB[2];
  }PRG_Banks;
  struct{
    Hbyte* Base0000_4KB[2];
  }CHR_Banks;
}H_NES_Cartridge_Mapper1;

//Mapper2
typedef struct{
  struct{
    Hbyte* Base8000_16KB[2];
  }PRG_Banks;
}H_NES_Cartridge_Mapper2;

//Mapper3
typedef struct{
  struct{
    Hbyte* Base0000_8KB;
  }CHR_Banks;
}H_NES_Cartridge_Mapper3;

//Mapper4
typedef struct{
  Hbyte Reg_8000;
  Hbyte Reg_A001;
  Hbyte Reg_C000;

  Hbyte IRQ_Timer_Cnt;
  Hbyte IRQ_Timer_IsReload;
  Hbyte IRQ_Enable;

  Hbyte R[8];
  

  struct{
    Hbyte* Base8000_8KB[4];
  }PRG_Banks;
  struct{
    Hbyte* Base0000_1KB[8];
  }CHR_Banks;

  union{
    struct{
      Hbyte* CHR_RAM;
      int ProgressSize;
      void (*DeInit)(void*);
      void (*Get)(void*,void*);
      void (*Load)(void*,void*);
    }Mapper74;
  }Variant;
}H_NES_Cartridge_Mapper4;

//Mapper7
typedef struct{
  Hbyte Reg_8000;
  Hbyte VRAM_ScreenType;
  Hbyte* Base8000;
}H_NES_Cartridge_Mapper7;

//Mapper21
typedef struct{
  Hbyte Reg_800x;
  Hbyte Reg_9002;
  Hbyte Reg_A00x;
  Hbyte MskLBit;
  Hbyte Reg_B000_E003[16];
  Hbyte MskHBit;

  Hbyte Reg_F002;
  Hbyte Count;
  Hbyte CountReload;
  Hint16 DivCount;
  


  struct{
    Hbyte* Base8000_8KB[4];
  }PRG_Banks;
  struct{
    Hbyte* Base0000_1KB[8];
  }CHR_Banks;
}H_NES_Cartridge_Mapper21;

//Mapper65
typedef struct{
  Hbyte Reg_8000;
  Hbyte Reg_B000_B007[8];
  Hbyte Reg_9000;
  Hbyte Reg_9003;
  Hbyte Reg_A000;
  
  Huint16 Count;
  Huint16 CountReload;
  
  struct{
    Hbyte* Base8000_8KB[4];
  }PRG_Banks;
  struct{
    Hbyte* Base0000_1KB[8];
  }CHR_Banks;
}H_NES_Cartridge_Mapper65;

//Mapper85
typedef struct{
  Hbyte Reg_PRGSEL[3];
  Hbyte MskBit;
  Hbyte Reg_CHRSEL[8];
  Hbyte Reg_E000;

  Hbyte Reg_F000;
  Hbyte Count;
  Hbyte CountReload;
  Hint16 DivCount;

  Hbyte IRQ_DelayCount;

  struct{
    Hbyte* Base8000_8KB[4];
  }PRG_Banks;
  struct{
    Hbyte* Base0000_1KB[8];
  }CHR_Banks;
}H_NES_Cartridge_Mapper85;




typedef union{
  H_NES_Cartridge_Mapper1 Mapper1;
  H_NES_Cartridge_Mapper2 Mapper2;
  H_NES_Cartridge_Mapper3 Mapper3;
  H_NES_Cartridge_Mapper4 Mapper4;
  H_NES_Cartridge_Mapper7 Mapper7;
  H_NES_Cartridge_Mapper21 Mapper21;
  //Mapper23 使用 Mapper21
  H_NES_Cartridge_Mapper65 Mapper65;
  //Mapper74 使用 Mapper4
  H_NES_Cartridge_Mapper85 Mapper85;
}H_NES_Cartridge_Mapper;




typedef struct{

  struct{
    Hbyte* PRG_ROM;
    Huint32 PRG_ROM_Size;
    Hbyte* CHR_ROM;
    Huint32 CHR_ROM_Size;
    Hbyte* PRG_RAM;
    Hint32 PRG_RAM_Size;
    Hbyte* CHR_RAM;
    Hint32 CHR_RAM_Size;

    void (*DeInit)(void*);

    H_NES_Cartridge_Mapper SpecificMapper;
  }Mapper;
  

  Hbyte (*PRG_Read)(Huint16,void*);
  void (*PRG_Write)(Hbyte,Huint16,void*);
  Huint16 (*PRG_Read16)(Huint16,void*);

  Hbyte (*CHR_Read)(Huint16,void*);
  void (*CHR_Write)(Hbyte,Huint16,void*);

  struct{
    int Size;
    void (*Get)(void*,void*);
    void (*Load)(void*,void*);
  }Progress;

  struct{
    void (*MMC3_A12_Rise)(unsigned int,void*);
    void (*CPUTick)(int,void*);
    void (*Reset)(void*);
  }Option;

}H_NES_Cartridge;




#endif //__Cartridge_H_NES_Cartridge_H_
